﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Warehouse.Api;
using W.Warehouse.Api.Application.Command.ReportlossesCommand;
using W.Warehouse.Domain.Reportlosses;
using W.Warehouse.Read.Api.Application.Command.ReportlossesCommand;

namespace W.Warehouse.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]

    //报损管理
    public class ReportlossesController : ControllerBase
    {
        private readonly IMediator mediator;
        private ILogger<ReportlossesController> logger;

        public ReportlossesController(IMediator mediator, ILogger<ReportlossesController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 添加报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddReportlosses(ReportlossesCreateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.UpdateName = name;
            command.userid = Convert.ToInt32(uid);
            command.officeId = sid;
            return await mediator.Send(command);
        }

        /// <summary>
        /// 删除报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelReportlosses(ReportlossesDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("删除报损管理");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }

        /// <summary>
        /// 修改报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpReportlosses(ReportlossesUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("修改报损管理");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }

        /// <summary>
        /// 反填报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<ReportlossesInfo>> FindReportlosses(ReportlossesFindCommand command)
        {
            ApiResult<ReportlossesInfo> result = new ApiResult<ReportlossesInfo>();
            try
            {
                logger.LogInformation("反填报损管理");
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }

        /// <summary>
        /// 审核报损管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ReportAuid(ReportlossEsexamineCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("审核报损管理");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }


        /// <summary>
        /// 报损批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RepoRangeInfo(ReportlossesRangeDelCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
